<?php
$cmsDao = new cms_controller();
if (isset($_GET['do']) && $_GET['do'] == 'del') {
    $id = $_GET['id'];
    $del_cms = $cmsDao->deleteCmsById($id);
}
if(isset($_GET['action']) && ((isset($_GET['do'])) && ($_GET['do']=='add') || ($_GET['do']=='edit')))
{
	$width = 0; $color = ""; $message = "";
	$codeInfo = "";
	$titleInfo = "";
	$contentInfo = "";
    $codevalue = "";
	$titlevalue = "";
	$contentvalue = "";
	$class_input_code = "inp-form";
	$class_input_title = "inp-form";
	if ($_GET['do'] == 'add')$page_heading = "Add New CMS"; else $page_heading = "Edit CMS";
	if(isset($_POST['submit'])){
		$width = 100;
		if((!validateTitle($_POST['txt_title'])) || (!validateCode($_POST['txt_code'])) || (!validateContent($_POST['txt_content']))){
			$color = "red";
			$codevalue = $_POST['txt_code'];
			$titlevalue = $_POST['txt_title'];
			$contentvalue = $_POST['txt_content'];
			if ($_GET['do'] == 'add') $message = "Add CMS Failed !"; else $message = "Edit CMS Failed !";
			if(!validateCode($_POST['txt_code'])){ $codeInfo = "The Code must more than 3 letters !"; $class_input_code = "inp-form-error"; }
			if(!validateTitle($_POST['txt_title'])){ $titleInfo = "The Title must more than 3 letters !"; $class_input_title = "inp-form-error"; }
			if(!validateContent($_POST['txt_content'])) $contentInfo = "* Content is not null !";
		}else{
			$color = "green";
			if ($_GET['do'] == 'add') {
				$cmsModel = new cms('',$_POST['txt_code'],$_POST['txt_title'],$_POST['txt_content']);
				if($cmsDao->addCms($cmsModel))$message = "Add CMS Success !";
			}elseif ($_GET['do'] == 'edit') {
				$id = $_GET['id'];
				$cmsModel = new cms($id,$_POST['txt_code'],$_POST['txt_title'],$_POST['txt_content']);
				$cmsDao->editCms($cmsModel);
				$message = "Edit CMS Success !";
				$cmsModel = $cmsDao->getCmsById($id);
				$codevalue = $cmsModel->code;
				$titlevalue = $cmsModel->title;
				$contentvalue = $cmsModel->content;
			}	
		}	
	}else{
        if ($_GET['do'] == 'edit') {
		$id = $_GET['id'];
		$cmsModel = $cmsDao->getCmsById($id);
		$codevalue = $cmsModel->code;
		$titlevalue = $cmsModel->title;
		$contentvalue = $cmsModel->content;
		}
	}
?>
<div id="content-outer">
<!-- start content -->
	<div id="content">
		<div id="page-heading">
			<h1><?php echo $page_heading?></h1>
		</div>
			<table border="0" width="100%" cellpadding="0" cellspacing="0" id="content-table">
				<tr>
					<th rowspan="3" class="sized"><img src="images/shared/side_shadowleft.jpg" width="20" height="300" alt="" /></th>
					<th class="topleft"></th>
					<td id="tbl-border-top">&nbsp;</td>
					<th class="topright"></th>
					<th rowspan="3" class="sized"><img src="images/shared/side_shadowright.jpg" width="20" height="300" alt="" /></th>
				</tr>
				<tr>
					<td id="tbl-border-left"></td>
					<td>
					<!--  start content-table-inner -->
						<div id="content-table-inner">
							<div id="message-<?php echo $color?>">
								<table border="0" width="<?php echo $width."%"?>" cellpadding="0" cellspacing="0">
									<tr>
										<td class="<?php echo $color?>-left"><?php echo $message?></td>
										<td class="<?php echo $color?>-right">
											<a class="close-<?php echo $color?>"><img src="../images/table/icon_close_<?php echo $color?>.gif"   alt="" />											</a>
										</td>
										</tr>
								</table>
							</div>   
							<form action="" method="POST">
								<table border="0" width="100%" cellpadding="0" cellspacing="0">
									<tr valign="top">
										<td>
											<table border="0" cellpadding="0" cellspacing="0"  id="id-form">
												<tr>
													<th valign="top">Code :</th>
													<td>
														<input id="txt_code" name="txt_code" type="text" class="<?php echo $class_input_code?>" value="<?php echo $codevalue?>" />
														<span id="codeInfo" class="error"><?php echo $codeInfo?></span>
													</td>
												</tr>
												<tr>
													<th valign="top">Title:</th>
													<td>
														<input id="txt_title" name="txt_title" type="text" class="<?php echo $class_input_title?>" value="<?php echo $titlevalue; ?>" />
														<span id="titleInfo" class="error"><?php echo $titleInfo?></span>
													</td>
												</tr>
												<tr>
													<th valign="top">Content:</th>
													<td>
														<span id="contentInfo" class="error"><?php echo $contentInfo?></span>
														<textarea cols="100" id="txt_content" name="txt_content" rows="20"><?php echo $contentvalue ?></textarea>
													</td>
														<?php
														// Include the CKEditor class.
														include_once "../ckeditor/ckeditor.php";
														
														// Create a class instance.
														$CKEditor = new CKEditor();
														
														// Path to the CKEditor directory.
															$CKEditor->basePath = '';
														// Change default textarea attributes.
														$CKEditor->config['width'] = 1000;
														$CKEditor->config['height'] = 300;
														// Replace a textarea element with an id (or name) of "textarea_id".
														$CKEditor->replace("txt_content");
														?>         
												</tr>
												<tr>
													<th>&nbsp;</th>
													<td valign="top">
															<input type="submit" name="submit" value="submit" class="form-submit" />
															<a onclick="location='index.php?action=cms&page=<?php echo $_SESSION['page']+1;?>&row=<?php echo $_SESSION['row']?>'" class="form-cancel">Cancel</a>
															<input type="reset" value="" class="form-reset" />
													</td>
													<td></td>
												</tr>
											</table>
										</td>
										<!-- end id-form  -->
									</tr>
									<tr>
										<td><img src="../images/shared/blank.gif" width="695" height="1" alt="blank" /></td>
									</tr>
								</table>
							</form>
							<div class="clear"></div>
						</div>
					</td>
							<!--  end content-table-inner  -->
					<td id="tbl-border-right"></td>
				</tr>
				<tr>
					<th class="sized bottomleft"></th>
					<td id="tbl-border-bottom">&nbsp;</td>
					<th class="sized bottomright"></th>
				</tr>
			</table>
		<div class="clear">&nbsp;</div>
	</div>
<!--  end content -->
	<div class="clear">&nbsp;</div>
</div>
<?php }else{ 
$_SESSION['page']=$_GET['page']-1;
$_SESSION['row']=$_GET['row'];
?>
<div id="content-outer">
<!-- start content -->
<div id="content">
	<!--  start page-heading -->
	<div id="page-heading">
            <h1><a style="color: #2e74b2; cursor: pointer;" onclick="location='index.php?action=cms&do=add'">Add CMS</a></h1>
	</div>
	<!-- end page-heading -->
	<table border="0" width="100%" cellpadding="0" cellspacing="0" id="content-table">
	<tr>
		<th rowspan="3" class="sized"><img src="../images/shared/side_shadowleft.jpg" width="20" height="300" alt="" /></th>
		<th class="topleft"></th>
		<td id="tbl-border-top">&nbsp;</td>
		<th class="topright"></th>
		<th rowspan="3" class="sized"><img src="../images/shared/side_shadowright.jpg" width="20" height="300" alt="" /></th>
	</tr>
	<tr>
		<td id="tbl-border-left"></td>
		<td>
		<!--  start content-table-inner ...................................................................... START -->
		<div id="content-table-inner">
                        	<!--  start message-green -->
							    
                       <!--  start message-green -->
							    
				<!--  end message-green -->
                            
				<!--  end message-green -->
                                        
			<!--  start table-content  -->
			<div id="table-content">
			
				<!--  start product-table ..................................................................................... -->
				<form id="mainform" action="">
				<table border="0" width="100%" cellpadding="0" cellspacing="0" id="product-table">
				<tr>
					<th class="table-header-check"><a id="toggle-all" ></a> </th>
					<th class="table-header-repeat line-left minwidth-1"><a href="">Code </a>	</th>
					<th class="table-header-repeat line-left minwidth-1"><a href="">Title </a></th>
					<th class="table-header-options line-left"><a href="">Options</a></th>
				</tr>
                                <?php
                                $page = $_GET['page']-1;
                                $row = $_GET['row'];
                                $lastpage = (int)(($cmsDao->countPageCms()-1)/$_GET['row'])+1;
                                if($page>=$lastpage) header("Location:../backend/index.php?action=cms&page=1&row=".$row);
                                $listCms = $cmsDao->pagingCms($page,$_GET['row']);
                                    foreach ($listCms as $temp) 
                                    {
                                        ?>
                                        <form action="" method="POST">
                                            <tr>
                                                <td><input type="hidden" name="id" value="<?php echo $temp->id; ?>"><input  type="checkbox"/></td>
                                                
                                                <td><?php echo $temp->code; ?></td>
                                                <td><?php echo $temp->title; ?></td>                                               
                                                <td>
                                                    <a title="Edit" onclick="location='index.php?action=cms&do=edit&id=<?php echo $temp->id; ?>'" class="icon-1 info-tooltip"></a>
                                                    <a title="Delete" onclick="location='index.php?action=cms&do=del&id=<?php echo $temp->id; ?>&page=<?php echo $page+1?>&row=<?php echo $row?>'" class="icon-2 info-tooltip"></a>
                                                </td>
                                            </tr> 
                                        </form>
                                    <?php
                                    }
                                    ?>
				</table>
				<!--  end product-table................................... --> 
				</form>
			</div>
			<table border="0" cellpadding="0" cellspacing="0" id="paging-table">
			<tr>
			<td>
				<a onclick="location='index.php?action=cms&page=1&row=<?php echo $row?>'" class="page-far-left"></a>
				<a onclick="location='index.php?action=cms&page=<?php if($page>0) echo $page; else echo 1;?>&row=<?php echo $row?>'" class="page-left"></a>
				<div id="page-info">Page <strong id="page-current"><?php echo $page+1 ?></strong> / <?php echo $lastpage;?></div>
				<a onclick="location='index.php?action=cms&page=<?php if($page+2<$lastpage) echo $page+2; else echo $lastpage;?>&row=<?php echo $row?>'" class="page-right"></a>
				<a onclick="location='index.php?action=cms&page=<?php echo $lastpage;?>&row=<?php echo $row?>'" class="page-far-right"></a>
			</td>
			<td>
			<select  class="styledselect_pages">
				<option value="">Number of rows</option>
				<option value="">5</option>
				<option value="">10</option>
				<option value="">15</option>
			</select>
			</td>
			</tr>
			</table>
			<!--  end paging................ -->	
			<div class="clear"></div>
		</div>
		<!--  end content-table-inner ............................................END  -->
		</td>
		<td id="tbl-border-right"></td>
	</tr>
	
	</table>
	<div class="clear">&nbsp;</div>

</div>
<!--  end content -->
<div class="clear">&nbsp;</div>
</div>
<?php
}
?> 
<script type="text/javascript" src="../js/validcms.js"></scrip